﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Data.SqlTypes;

namespace IHISCETeim.DAL
{
    public class UMacKeepDAL
    {
        public static int Add(UMacKeepInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeep_Add",obj.SerialNumber.ToUpper().Trim(),obj.IsWarrantyCard,obj.IntoDate,obj.IntoHours,obj.CustomerName,obj.CustomerMobile,obj.CustomerAddress,obj.AgentName,obj.AgentTel,obj.Remark,obj.FirstCostDate,obj.FirstCost,obj.SecondCostDate,obj.SecondCost,obj.CostRemark,obj.SrvActRealName);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(string SerialNumber)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeep_Delete", SerialNumber.ToUpper().Trim());
            try{
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetBySerialNumber(string SerialNumber)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeep_GetList", SerialNumber.ToUpper().Trim());
            return DB.ExecuteReader(dbCommand);
	    }
        public static DataTableCollection GetPagination(string sqlwhere, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();
         
            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeep_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String, sqlwhere);
         
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
            dbCommand.CommandTimeout = 300000;
            DataTableCollection dt = DB.ExecuteDataSet(dbCommand).Tables;
           
            TotalCount=Convert.ToInt32(DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        public static bool Update(UMacKeepInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeep_Update",obj.UMKID, obj.SerialNumber.ToUpper().Trim(),obj.IsWarrantyCard,obj.IntoDate,obj.IntoHours,obj.CustomerName,obj.CustomerMobile,obj.CustomerAddress,obj.AgentName,obj.AgentTel,obj.Remark,obj.FirstCostDate,obj.FirstCost,obj.SecondCostDate,obj.SecondCost,obj.CostRemark);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        public static DataTable GetAgentMacNum()
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeep_GetAgentMacNum");
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
        public static int UMacKeepDetail_Add(UMacKeepDetailInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeepDetail_Add", obj.SerialNumber.ToUpper().Trim(), obj.WorkTime, obj.KeepDate, obj.KeepHours,obj.ScanImg,obj.JHScanImg,obj.TimerScanImg,obj.BYJScanImg,obj.HYScanImg, obj.IsSHChange, obj.IsSHChangeText, obj.IsSHWater, obj.IsSHWaterText, obj.IsSHGuiHan, obj.IsSHGuiHanText,obj.IsFirstOil,obj.IsFirstOilText, obj.IsVerify);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool UMacKeepDetail_Delete(int UMKDID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeepDetail_Delete", UMKDID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static IDataReader UMacKeepDetail_GetByID(int UMKDID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeepDetail_GetByID", UMKDID);
            return DB.ExecuteReader(dbCommand);
        }

        public static bool UMacKeepDetail_Update(UMacKeepDetailInfo obj)
        {
            try
            {
                Database DB = DatabaseFactory.CreateDatabase();
                DbCommand dbCommandDel = DB.GetStoredProcCommand("UMacKeepDetail_Delete", obj.UMKDID);
                DB.ExecuteNonQuery(dbCommandDel);

                DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeepDetail_Add", obj.SerialNumber.ToUpper().Trim(), obj.WorkTime, obj.KeepDate, obj.KeepHours, obj.ScanImg, obj.JHScanImg, obj.TimerScanImg, obj.BYJScanImg, obj.HYScanImg, obj.IsSHChange, obj.IsSHChangeText, obj.IsSHWater, obj.IsSHWaterText, obj.IsSHGuiHan, obj.IsSHGuiHanText,obj.IsFirstOil,obj.IsFirstOilText, obj.IsVerify);
                return Convert.ToInt32(DB.ExecuteScalar(dbCommand))>0;
            }
            catch
            {
                return false;
            }

        }

        public static IDataReader UMacKeepDetail_GetBySerialNumber(string SerialNumber)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeepDetail_GetBySerialNumber", SerialNumber.ToUpper().Trim());
            return DB.ExecuteReader(dbCommand);
        }

        #region 短信相关
        public static int UMacKeepAlarm_Add(UMacKeepAlarmInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeepAlarm_Add", obj.SerialNumber, obj.KeepHours, obj.AlarmDate, obj.AlarmSMSContent);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool UMacKeepAlarm_Delete(int UMKAID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeepAlarm_Delete", UMKAID);
            try { DB.ExecuteScalar(dbCommand); return true; }
            catch { return false; }
        }
        public static DataTable UMacKeepAlarm_GetALL()
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeepAlarm_GetALL");
            return DB.ExecuteDataSet(dbCommand).Tables[0];
	    }

        public static IDataReader UMacKeepAlarm_GetBySerialNumber(string SerialNumber)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeepAlarm_GetBySerialNumber", SerialNumber);
            return DB.ExecuteReader(dbCommand);
        }

        public static bool UMacKeepAlarm_UpdateSendSMS(int UMKAID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeepAlarm_UpdateSendSMS", UMKAID);
            try
            {
                 DB.ExecuteScalar(dbCommand);
                 return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        public static IDataReader UMacKeepConfig_GetALL()
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeepConfig_GetALL");
            return DB.ExecuteReader(dbCommand);
	    }
        public static bool UMacKeepConfig_Update(UMacKeepConfigInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeepConfig_Update", obj.UMKCID, obj.KeepHours, obj.KeepDay, obj.Tkey, obj.Remark);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        #endregion

        /// <summary>
        /// 定期服务实施准确率
        /// </summary>
        /// <param name="f"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static DataTable GetDQHWSSZQL(string f, string t)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeep_GetDQHWSSZQL",f,t);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }

        public static DataTable GetZQL(string f, string t)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UMacKeep_GetZQL", f, t);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
    }
}
